//
//  ArticlesDatabase.m
//  chanyou
//
//  Created by leon on 15/1/9.
//  Copyright (c) 2015年 wanli. All rights reserved.
//

#import "ArticlesDatabase.h"
#import "ArticlesModel.h"
#import "DB.h"
#import "Singleton.h"
#import <ShareSDK/ShareSDK.h>
@implementation ArticlesDatabase

+ (BOOL)insertIntoTravelWithArticlesModel:(ArticlesModel *)articles
{
    sqlite3 *dbPoint = [DB openDB];
    
    [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
        if (result) {
            NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
            // 注销之后要将此处清除
            [def setObject:[userInfo uid] forKey:@"user"];
        }
    }];
    NSUserDefaults *def = [NSUserDefaults standardUserDefaults];
    NSString *userId = [def objectForKey:@"user"];
    NSString *sqlStr = [NSString stringWithFormat:@"insert into Articles(weibo_id,articles_id,articles_name,articles_photo_url) values(%ld,%ld,'%@','%@')", [userId integerValue], articles.sectionId, articles.name, articles.image_url];

    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"INSERT OK!");
        return YES;
    } else {
        NSLog(@"INSERT ERROR!");
        return NO;
    }

}

+ (NSInteger)searchWeiboId
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = @"select weibo_id from Articles";
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    
    NSInteger weiboID = 0;
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            unsigned int weibo = sqlite3_column_int(stmt, 0);
            weiboID = weibo;
        }
    }
    sqlite3_finalize(stmt);
    return weiboID;
}

+ (NSArray *)searchAllArticlesWithWeiboId:(NSInteger)weiboId
{
    sqlite3 *dbPoint = [DB openDB];
    sqlite3_stmt *stmt = nil;
    NSString *sqlStr = [NSString stringWithFormat:@"select articles_id,articles_name,articles_photo_url from Articles where weibo_id = %ld", weiboId];
    
    int result = sqlite3_prepare_v2(dbPoint, [sqlStr UTF8String], -1, &stmt, nil);
    NSMutableArray *array = [NSMutableArray array];
    if (result == SQLITE_OK) {
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            int articlesId = sqlite3_column_int(stmt, 0);
            const unsigned char *name =  sqlite3_column_text(stmt, 1);
            const unsigned char *url = sqlite3_column_text(stmt, 2);
            NSString *articlesName = [NSString stringWithUTF8String:(const char *)name];
            NSString *articlesUrl = [NSString stringWithUTF8String:(const char *)url];
            
            ArticlesModel *articles = [ArticlesModel articlesWithName:articlesName url:articlesUrl  sectionId:articlesId];
            [array addObject:articles];
        }
    }
    sqlite3_finalize(stmt);
    return array;
}

+ (void)deleteFromArticles:(NSInteger)articlesId
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Articles where articles_id = %ld", articlesId];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}

+ (void)deleteFromArticles:(NSInteger)articlesId weiboId:(NSInteger)weiboId
{
    sqlite3 *dbPoint = [DB openDB];
    NSString *sqlStr = [NSString stringWithFormat:@"delete from Articles where articles_id = %ld and weibo_id = %ld", articlesId, weiboId];
    int result = sqlite3_exec(dbPoint, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除成功");
    }else {
        NSLog(@"删除失败");
    }
}


@end
